package com.lagou.rabbitmqtask.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue queue() {
        //设置过期时间，死信交换器和死信路由Key
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 12000);
        args.put("x-dead-letter-exchange", "exchange.dlx");//死信交换器
        args.put("x-dead-letter-routing-key", "routing.key.dlx");//路由key
        return new Queue("queue.task61.topay", false, false, false, args);
    }

    @Bean
    public Queue unPayedQueue() {
        return new Queue("queue.task61.unpay", false, false, false, null);
    }

    @Bean
    public Queue payedQueue() {
        return new Queue("queue.task61.pay", false, false, false, null);
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue("queue.task61.dlx", false, false, false, null);
    }

    @Bean
    public Exchange dlxExchange() {
        return new DirectExchange("exchange.dlx", true, false);
    }

    @Bean
    public Exchange topayExchange() {
        return new FanoutExchange("exchange.task61.topay", true, false, null);
    }

    @Bean
    public Exchange payExchange() {
        return new FanoutExchange("exchange.task61.pay", true, false, null);
    }

    @Bean
    public Exchange unpayExchange() {
        return new FanoutExchange("exchange.task61.unpay", true, false, null);
    }

    @Bean
    public Binding dlxBinding() {
        return new Binding("queue.task61.dlx", Binding.DestinationType.QUEUE, "exchange.dlx", "routing.key.dlx", null);
    }

    @Bean
    public Binding bindingUnPay() {
        //超时未支付的订单
        return new Binding("queue.task61.unpay", Binding.DestinationType.QUEUE, "exchange.task61.unpay", "routing.key", null);
    }

    @Bean
    public Binding bindingToPay() {
        //等待支付的订单
        return new Binding("queue.task61.topay", Binding.DestinationType.QUEUE, "exchange.task61.topay", "routing.key", null);
    }

    @Bean
    public Binding bindingPay() {
        //已支付的订单
        return new Binding("queue.task61.pay", Binding.DestinationType.QUEUE, "exchange.task61.pay", "routing.key", null);
    }
}
